//
//  SSAlertView.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/4/24.
//

import UIKit

class SSAlertView: UIView {

    //标题
    lazy var label_title:UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.numberOfLines = 0
        label.font = UIFont.systemFont(ofSize: 16)
//        label.textColor = UIColor.init(hex: "#333333")
        return label
    }()
    //提示信息
    lazy var label_message:UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.numberOfLines = 0
        label.font = UIFont.systemFont(ofSize: 16)
//        label.textColor = UIColor.init(hex: "#3C484D")
        return label
    }()
    var isShowCancle = false    //是否显示取消按钮
    //取消按钮
    lazy var cancleBtn:UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(UIColor.init(hex: "#666666"), for: .normal)
        btn.setTitle("取消", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.addTarget(self, action: #selector(cancleBtnClicked), for: .touchUpInside)
        return btn
    }()
    //确定按钮
    lazy var confirmBtn:UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(UIColor.themeColor, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.setTitle("确定", for: .normal)
        btn.addTarget(self, action: #selector(confirmBtnClicked), for: .touchUpInside)
        return btn
    }()
    
    var confirmHandle:(()->())?
    var cancleHandle:(()->())?
    
    lazy var bgView:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.init(hex: "000000")
        view.alpha = 0.6
        
//        let tap = UITapGestureRecognizer(target: self, action: #selector(hide))
//        view.addGestureRecognizer(tap)
        
        return view
    }()
    init(title:String = "提示", message:String, cancleTitle:String = "取消", confirmTitle:String = "确定") {
        super.init(frame: CGRect(x: 0, y: 0, width: 270, height: 146))
        backgroundColor = UIColor.white
        layer.cornerRadius = 8
        layer.masksToBounds = true
        
        var height:CGFloat = 24
        if title.count > 0 {
            
            let titleHeight = title.sizeWithText(font: UIFont.systemFont(ofSize: 16, weight: .medium), size: CGSize(width: 230, height: CGFloat.zero)).height
            label_title.text = title
            label_title.frame = CGRect(x: 20, y: 24, width: 230, height: titleHeight)
            height += titleHeight
            height += 12
            addSubview(label_title)
        }
        else{
            height = 20
        }
        
        if message.count > 0{
            let messageHeight = message.sizeWithText(font: UIFont.systemFont(ofSize: 16, weight: .medium), size: CGSize(width: 230, height: CGFloat.zero)).height
            label_message.text = message
            label_message.frame = CGRect(x: 20, y: height, width: 230, height: messageHeight)
            height += messageHeight
            height += title.count > 0 ? 30 : 16
            addSubview(label_message)
        }
        
//        if cancleTitle.count > 0{
//            self.isShowCancle = true
//            cancleBtn.setTitle(cancleTitle, for: .normal)
//            cancleBtn.frame = CGRect(x: 0, y: height, width: 135, height: 43)
//            addSubview(cancleBtn)
//        }
        
        if confirmTitle.count > 0{
            let x = self.isShowCancle ? 135 : 0
            let width = self.isShowCancle ? 135 : 270
            confirmBtn.setTitle(confirmTitle, for: .normal)
            confirmBtn.frame = CGRect(x: x, y: Int(height), width: width, height: 43)
            addSubview(confirmBtn)
        }
        
        height += 43
        self.frame.size.height = height
        
        addLine()
        
    }
    
    func addLine2(originPoint: CGPoint, toPoint: CGPoint, color: UIColor, width: CGFloat) -> CAShapeLayer {
        let bezierPath = UIBezierPath()
        bezierPath.move(to: originPoint)
        bezierPath.addLine(to: toPoint)
        
        let shapeLayer = CAShapeLayer()
        // 线宽度
        shapeLayer.lineWidth = width;
        shapeLayer.strokeColor = color.cgColor
        shapeLayer.fillColor = UIColor.clear.cgColor
        // 添加路径
        shapeLayer.path = bezierPath.cgPath
        return shapeLayer
    }
    
    func addLine(){
        let shapeLayer = addLine2(originPoint: CGPoint(x: 0, y: self.frame.height - 43), toPoint: CGPoint(x: self.frame.width, y: self.frame.height - 43), color: UIColor.init(hex: "DFE4E8")!, width: 1)
        self.layer.addSublayer(shapeLayer)
        
        if isShowCancle{
            let shapeLayer1 = addLine2(originPoint: CGPoint(x: 135, y: self.frame.height - 43), toPoint: CGPoint(x: 135, y: self.frame.height), color: UIColor.init(hex: "DFE4E8")!, width: 1)
            self.layer.addSublayer(shapeLayer1)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    @objc func cancleBtnClicked(){
        if let _ = cancleHandle{
            cancleHandle!()
        }
        hide()
    }
    @objc func confirmBtnClicked(){
        if let _ = confirmHandle{
            confirmHandle!()
        }
        hide()
    }
    func showAlert(confirm:(()->())?, cancle: (()->())?) {
        let keywindow = UIApplication.shared.keyWindow
        confirmHandle = confirm
        cancleHandle = cancle
        bgView.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        keywindow?.addSubview(self.bgView)
        
        
        var rect = self.frame
        rect.origin.x = (UIScreen.main.bounds.width - rect.width) / 2
        rect.origin.y = (UIScreen.main.bounds.height - rect.height) / 2
        self.frame = rect
        keywindow?.insertSubview(self, aboveSubview: self.bgView)
        
    }
    
    @objc func hide(){
        UIView.animate(withDuration: 0.3, animations: {
            self.alpha = 0
            self.bgView.alpha = 0
        }) { (_) in
            self.alpha = 1
            self.bgView.alpha = 0.6
            self.bgView.removeFromSuperview()
            self.removeFromSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
